import { post, getStorageUser } from '@/lib/Util'
import { GET_TAGS, SET_TAGS, SEND_MSG, GET_DB, SEND_MSG_EX } from '@/lib/ApiURL'

/**
 * getHmiData - description
 *
 * @param  {string} page       所属页面唯一标识符
 * @param  {Function} resultFunc 成功时的回调
 * @param  {Function} errorFunc  错误时的回调
 * @param  {number} f          查询频率，默认是1000ms
 * @return {type}            description
 */
export async function getDB(postData, resultFunc, errorFunc) {
  /* if (postData.request.db !== undefined) {
    postData.request.db = 'PanSteel'
  }*/
  const requestData = {}
  const user = getStorageUser()
  const userName = user ? user.name : ''
  requestData.action = GET_DB
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = [{}]
  requestData.body[0].module = postData.module
  requestData.body[0].method = postData.method
  requestData.body[0].request = postData.request
  requestData.body[0].record = false // postData.record
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}

export async function DBFile(postData, resultFunc, errorFunc) {
  const requestData = {}
  const user = getStorageUser()
  const userName = user ? user.name : ''
  requestData.action = GET_DB
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = [{}]
  requestData.body[0].module = postData.module
  requestData.body[0].method = postData.method
  requestData.body[0].request = postData.request
  requestData.body[0].record = false // postData.record
  const result = await post('FormHMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}

export async function getTagsName(postData, resultFunc, errorFunc) {
  const requestData = prepareRequestData(postData, GET_DB)
  requestData.action = GET_DB
  requestData.body[0].module = 'TagService'
  requestData.body[0].method = 'GetTagNames'
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}
export async function getTags(postData, resultFunc, errorFunc) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  const requestData = {}
  requestData.action = GET_TAGS
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = postData
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}
export async function setTags(postData) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  const requestData = {}
  requestData.action = SET_TAGS
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = postData
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  }
}
export async function sendMsg(postData, resultFunc, errorFunc) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  const requestData = {}
  requestData.action = SEND_MSG
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = postData
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}
export async function sendMsg86(postData, resultFunc, errorFunc) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  const requestData = {}
  requestData.action = SEND_MSG
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = postData
  const result = await post('/forward86', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}
export async function sendMsgEx(postData, resultFunc, errorFunc) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  const requestData = {}
  requestData.action = SEND_MSG_EX
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = postData
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result.code !== 200) {
    errorFunc(result.extraMsg)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}

export async function sendMsgZ(postData, resultFunc, errorFunc) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  const requestData = {}
  requestData.action = SEND_MSG_EX
  requestData.operator = userName
  requestData.terminal = userName
  requestData.body = postData
  const result = await post('/HMI', requestData)
  // const { result, err } = await post('/webservice/getHmiData.hmi', tags)
  if (result === undefined || result.code !== 200) {
    errorFunc(result)
    window.$nuxt.$store.commit('SET_ERROR_INFO', result)
    // 异常处理
  } else {
    // 回调
    resultFunc(result)
    // resultFunc(tags)
  }
}

/**
 * getVariablesByPage - 获取当前页面的查询命令 {tags:[], msgs:[]}
 *
 * @param  {type} page description
 * @return {type}      description
 */
/* function getVariablesByPage(page) {
  console.log('page', page)
  return JSON.parse(localStorage.getItem('hmi/' + page) || '{}')
}*/

function prepareRequestData(postData, action) {
  const user = getStorageUser()
  const userName = user ? user.name : ''
  return {
    action,
    operator: userName,
    terminal: userName,
    body: [
      {
        module: 'DbService',
        method: 'DbService',
        request: postData,
        record: false // true
      }
    ]
  }
}
